home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9971 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  46 lines

  1. Path: mozo.cc.purdue.edu!not-for-mail
  2. From: hrubin@b.stat.purdue.edu (Herman Rubin)
  3. Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
  4. Subject: Re: Access carry flag from C
  5. Date: 5 Mar 1996 07:26:16 -0500
  6. Organization: Purdue University Statistics Department
  7. Message-ID: <4hhbt8$1d3o@b.stat.purdue.edu>
  8. References: <Dn1C9z.DGv.0.net@indra.com> <825377932snz@genesis.demon.co.uk> <4h1veoINNlns@anvil.ugrad.cs.ubc.ca> <fgrieu-0403961143580001@ppp78.micronet.fr>
  9. NNTP-Posting-Host: b.stat.purdue.edu
  10.  
  11. In article <fgrieu-0403961143580001@ppp78.micronet.fr>,
  12. Franτois Grieu <fgrieu@micronet.fr> wrote:
  13. >In article <Dn1C9z.DGv.0.net@indra.com>,
  14. >Steve Sullivan <sullivan@indra.com> wrote:
  15.  
  16. >> Is it possible to determine if a fixed point overflow has
  17. >> occurred from within C?  For example:
  18. >>    i = j + k;
  19. >>    if (overflowed) ....;
  20.  
  21. >For the purprose of doing multiple precision arithmetic, the following
  22. >trick works across many machines and compilers, including 80x86, 680x0, PPCs
  23.  
  24. >unsigned long yl,yh;  /* double_unsigned_long_int y */
  25. >unsigned long x;
  26.  
  27. >/* add x to double_unsigned_long_int variable y */
  28. >   if (x + yl < yl) ++yh; /* test for overflow on next line */
  29. >   yl += x;
  30.  
  31.  
  32. >(I missed a lot of this thread, so maybe this has already been posted)
  33.  
  34. This will work, but slowly.  Anything can be done on any machine in any
  35. language, if one works hard enough, and has enough memory.  
  36.  
  37. Notice that in the above code, each addition must be performed twice.
  38. BTW, there is a weakness in the code; ++yh can cause a carry, so
  39. another test is needed.  As testing is expensive, what is needed in
  40. this case is not a test for carry, but the ability to add the carry
  41. bit to something.  If you check this out, you will see that this can
  42. be done.
  43. -- 
  44. Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907-1399
  45. hrubin@stat.purdue.edu     Phone: (317)494-6054    FAX: (317)494-0558
  46.